/**
 * 
 */
package com.ipay.utils;



/**
 * @author jxb
 *
 */
public class HexEncode extends Encoder {
	private static final char hex[] = {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'a', 'b', 'c', 'd', 'e', 'f'
    };

	public String encode(byte[] data) {
		StringBuffer ret = new StringBuffer(data.length * 2);
        for(int i = 0; i < data.length; i++) {
            ret.append(hex[data[i] >>> 4 & 0xf]);
            ret.append(hex[data[i] & 0xf]);
        }
        return ret.toString();
	}
	
	public byte[] decode(String s) {
		byte ret[] = new byte[s.length() / 2];
        for(int i = 0; i < ret.length; i++)
            ret[i] = (byte)Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);

        return ret;
	}
}
